package com.satan.fmmall.controller;

import com.satan.fmmall.cache.Cache;
import com.satan.fmmall.entity.ProductComments;
import com.satan.fmmall.service.ProductCommentsService;
import com.satan.fmmall.service.ProductService;
import com.satan.fmmall.vo.ResCode;
import com.satan.fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName ProductController
 * @Description TODO
 * @date 2022/4/7 14:52
 * @Version 1.0
 */
@Api(value = "提供商品信息相关的接口", tags = "商品管理")
@RequestMapping("/product")
@RestController
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private ProductCommentsService productCommentsService;
    @Cache(expire = 60*1000,name = "getProductBasicInfo")
    @ApiOperation("商品基本信息查询接口")
    @GetMapping("/detail-info/{pid}")
    public ResultVo getProductBasicInfo(@PathVariable("pid") String productId) {
        return productService.getProductBasicInfo(productId);
    }
    @Cache(expire = 60*1000,name = "getProductParams")
    @ApiOperation("商品参数信息查询接口")
    @GetMapping("/detail-params/{pid}")
    public ResultVo getProductParams(@PathVariable("pid") String productId) {
        return productService.getProductParamsById(productId);
    }
    @Cache(expire = 60*1000,name = "getProductCommonts")
    @ApiOperation("商品评论信息查询接口")
    @GetMapping("/detail-comments/{pid}")
    public ResultVo getProductCommonts(@PathVariable("pid") String pid, int pageNum, int pageSize) {
        return productCommentsService.listCommentsByProductId(pid, pageNum, pageSize);
    }

    @Cache(expire = 60*1000,name = "getProductCommontsCount")
    @ApiOperation("商品评价统计查询接口")
    @GetMapping("/detail-comments-count/{pid}")
    public ResultVo getProductCommontsCount(@PathVariable("pid") String productId) {
        System.out.println("productId:"+productId);
        return productCommentsService.getCommentsCountByProductId(productId);
    }
    @Cache(expire = 60*1000,name = "getProductsByCategoryId")
    @ApiOperation("根据类别查询商品接口")
    @GetMapping("/list-by-categoryid/{categoryId}")
    public ResultVo getProductsByCategoryId(@PathVariable("categoryId") Integer categoryId, @RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        return productService.getProductsByCategoryId(categoryId,pageNum,pageSize);
    }
    @Cache(expire = 60*1000,name = "getProductsByKeyword")
    @ApiOperation("根绝关键字查询商品接口")
    @GetMapping("/list-by-keyword")
    public ResultVo getProductsByKeyword(String keyword,int pageNum,int pageSize) throws IOException {
        return productService.getProductsByKeyword(keyword,pageNum,pageSize);
    }
    @ApiOperation("商品评论提交接口")
    @PostMapping("/comments-submit")
    public ResultVo submitProductComments(@RequestParam("orderId") String orderId,@RequestBody List<ProductComments> productCommentsList){
        System.out.println("orderId = " + orderId);
        ResultVo resultVo=null;
        try {
            productCommentsService.insertProductComments(productCommentsList,orderId);
            resultVo= ResultVo.success(null);
        } catch (SQLException e) {
            resultVo=  ResultVo.fail(ResCode.INSERT_ERROR);
        }
        return resultVo;
    }

}
